﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using XTeam.Document.Entities;
using XTeam.Documents.Business;
using XTeam.Document.Factory.ServiceInterface;
using XTeam.Core.Helper;

namespace XTeam.Document.Services
{
    public class ArticleClassifyService : IArticleClassifyService
    {

        private ArticlClassifyBLL _articleClassifyBll;

        /// <summary>
        /// 构造函数
        /// </summary>
        public ArticleClassifyService()
        {
            _articleClassifyBll = new ArticlClassifyBLL();
        }

        /// <summary>
        /// 主键查找
        /// </summary>
        public ArticleClassifyEntity Find(int classId = 0)
        {
            ArticleClassifyEntity root = new ArticleClassifyEntity();
            root.ClassId = 0;
            root.Floor = -1;
            root.ClassName = "Root";
            var classifyList = this.Search(null);
            TreeBuilder<ArticleClassifyEntity> tree = new TreeBuilder<ArticleClassifyEntity>(root);
            tree.Builder(classifyList);
            ArticleClassifyEntity node = classId == 0 ? root : classifyList.SingleOrDefault(i => i.ClassId == classId);

            return node;

        }
        /// <summary>
        /// 搜寻
        /// </summary>
        public IList<ArticleClassifyEntity> Search(Func<ArticleClassifyEntity, bool> filter)
        {
            return _articleClassifyBll.Search(filter);
        }

        /// <summary>
        /// 更新
        /// </summary>
        public int Update(ArticleClassifyEntity entity)
        {
            return this.Update(new ArticleClassifyEntity[] { entity });
        }

        /// <summary>
        /// 批量更新
        /// </summary>
        public int Update(IList<ArticleClassifyEntity> entities)
        {
            var classifyList = _articleClassifyBll.Search(null);
            TreeBuilder<ArticleClassifyEntity> tree = new TreeBuilder<ArticleClassifyEntity>(new ArticleClassifyEntity(), classifyList);
            tree.Update(ref entities);
            return _articleClassifyBll.Update(entities);
        }

        /// <summary>
        /// 新增
        /// </summary>
        public int Append(ArticleClassifyEntity entity)
        {
            return this.Append(new ArticleClassifyEntity[] { entity });
        }

        /// <summary>
        /// 批量增加
        /// </summary>
        public int Append(IList<ArticleClassifyEntity> entities)
        {
            var classifyList = _articleClassifyBll.Search(null);
            TreeBuilder<ArticleClassifyEntity> tree = new TreeBuilder<ArticleClassifyEntity>(new ArticleClassifyEntity(), classifyList);
            tree.Append(ref entities);
            _articleClassifyBll.Append(entities);
            tree.Update(ref entities);
            return _articleClassifyBll.Update(entities, i => i.Path);
        }

        /// <summary>
        /// 删除
        /// </summary>
        public int Delete(IList<int> ids)
        {
            List<ArticleClassifyEntity> classifyList = new List<ArticleClassifyEntity>();
            foreach (var item in ids)
            {
                classifyList.Add(new ArticleClassifyEntity()
                {
                    ClassId = item
                });

            }

            return _articleClassifyBll.Delete(classifyList);
        }

        /// <summary>
        /// 批量删除
        /// </summary>
        public int Delete(int classiyfId)
        {
            return this.Delete((new int[] { classiyfId }));
        }

        /// <summary>
        /// 根
        /// </summary>
        public ArticleClassifyEntity Root()
        {
            ArticleClassifyEntity root = new ArticleClassifyEntity();
            root.ClassId = 0;
            root.Floor = -1;
            root.ClassName = "Root";
            var classifyList = this.Search(null);
            TreeBuilder<ArticleClassifyEntity> tree = new TreeBuilder<ArticleClassifyEntity>(root);
            tree.Builder(classifyList);
            return root;
        }
    }
}
